package com.jingxi.portal.service;

import com.jingxi.common.pojo.JingXiResult;
import com.jingxi.common.util.HttpclientUtil;
import com.jingxi.common.util.JsonUtils;
import com.jingxi.model.TbItem;
import com.jingxi.portal.pojo.CartItem;
import com.jingxi.portal.utils.CookieUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

@Service
public class CartServiceImpl implements CartService {
//    @Value("${REST_BASE_URL}")
    private String REST_BASE_URL="http://localhost:8082/rest";
//    @Value("${ITME_INFO_URL}")
    private String ITME_INFO_URL="/item/info/";

    @Override
    public JingXiResult addCartItem(long itemId, int num, HttpServletRequest request, HttpServletResponse response) throws IOException {
        //取商品信息
        CartItem cartItem = null;
//取购物车商品列表
        List<CartItem> itemList = getCartItemList(request);
//判断购物车商品列表中是否存在此商品
        for (CartItem cItem : itemList) {
            //如果存在此商品
            if (cItem.getId() == itemId) {//增加商品数量
                cItem.setNum(cItem.getNum() + num);
                cartItem = cItem;
                break;
            }
        }
        if (cartItem == null) {
            cartItem = new CartItem();
            //根据商品id查询商品基本信息。
            String json = HttpclientUtil.get(REST_BASE_URL + ITME_INFO_URL + itemId);
            //把json转换成java对象
            JingXiResult jingXiResult = JingXiResult.formatToPojo(json, TbItem.class);
            if (jingXiResult.getStatus() == 200) {
                TbItem item = (TbItem) jingXiResult.getData();
                cartItem.setId(item.getId());
                cartItem.setTitle(item.getTitle());
                cartItem.setImage(item.getImage() == null ? "" : item.getImage().split(",")
                        [0]);
                cartItem.setNum(num);
                cartItem.setPrice(item.getPrice());
            }//添加到购物车列表
            itemList.add(cartItem);
        }
        //把购物车列表写入cookie
        CookieUtils.setCookie(request, response, "TT_CART", JsonUtils.objectToJson(itemList), true);
        return JingXiResult.ok();

    }

    @Override
    public List<CartItem> getCartItemList(HttpServletResponse response, HttpServletRequest request) {
        //从cookie中取商品列表
        String cartJson = CookieUtils.getCookieValue(request, "TT_CART", true);
        if (cartJson == null) {
            return new ArrayList<>();
        }
        //把json转换成商品列表
        try {
            List<CartItem> list = JsonUtils.jsonToList(cartJson, CartItem.class);
            return list;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return new ArrayList<>();
    }


    public List<CartItem> getCartItemList(HttpServletRequest request) {
        //从cookie中取商品列表
        String cartJson = CookieUtils.getCookieValue(request, "TT_CART", true);
        if (cartJson == null) {
            return new ArrayList<>();
        }
        //把json转换成商品列表
        try {
            List<CartItem> list = JsonUtils.jsonToList(cartJson, CartItem.class);
            return list;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return new ArrayList<>();
    }

    @Override
    public JingXiResult deleteCartItem(long itemId, HttpServletResponse response, HttpServletRequest request) {

        List<CartItem>itemList=getCartItemList(request);
        for (CartItem cartItem:itemList){
            if(cartItem.getId()==itemId){
                itemList.remove(cartItem);
                break;
            }
        }
        CookieUtils.setCookie(request,response,"TT_CART",JsonUtils.objectToJson(itemList),true);
        return JingXiResult.ok();
    }
}
